package org.amj.wn;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@SuppressWarnings("serial")
public class SelfServiceOperations extends HttpServlet {

	enum Operation {
		MODIFY, LOGIN;
	}

	class OperationInfo {
		private final String memberCode;
		private final String passCode;
		private final Map<String, String> otherValues;
		private final Operation operation;
		private OperationInfo(String memberCode, String passCode,
				Map<String, String> otherValues, Operation operation) {
			super();
			this.memberCode = memberCode;
			this.passCode = passCode;
			this.otherValues = otherValues;
			this.operation = operation;
		}
		public String getMemberCode() {
			return memberCode;
		}
		public String getPassCode() {
			return passCode;
		}
		public Map<String, String> getOtherValues() {
			return otherValues;
		}
		public Operation getOperation() {
			return operation;
		}


	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doGet(req, resp);
	}

	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		OperationInfo opInfo = getOperationInfo(req);
		if(opInfo != null && opInfo.getOperation() == Operation.LOGIN) {
			login(req, opInfo);
			if(WNUserService.isLoggedIn(req)) {
				resp.sendRedirect("/user/datasheet.jsp");
			} else {
				resp.sendRedirect("/user/login.jsp?r=2");
			}
		}
		else if(WNUserService.isLoggedIn(req)) {
			if(opInfo != null) {
				if(opInfo.getOperation() ==  Operation.MODIFY) {
					WaqfeNau.modify(opInfo.memberCode, opInfo.getOtherValues());
				}
			}

			resp.sendRedirect("/user/datasheet.jsp?_pn=" + req.getParameter("_pn"));
		}
		else
		{
			resp.sendRedirect("/user/login.jsp?r=1");
		}
	}

	private boolean login(HttpServletRequest req, OperationInfo opInfo) {
		WaqfeNau waqfeNau = WaqfeNau.get(opInfo.memberCode);
		if(waqfeNau == null || !waqfeNau.getPassCode().equals(opInfo.passCode))
			return false;
		req.getSession().setAttribute(WNUserService.CURRENT_USER, opInfo.memberCode);
		return true;
	}



	private OperationInfo getOperationInfo(HttpServletRequest req) {
		String operation = req.getParameter("opt");

		Operation opr = null;
		try {
			opr = Operation.valueOf(operation);
		}catch(Exception exp) { return null;}

		Property[] properties = Property.values();

		Map<String, String> values = new HashMap<String, String>();

		for (Property property : properties) {
			if(property.getUiElement() == UIElement.FIELD) {
				if(property != Property.memberCode && property != Property.passCode) {
					String value = req.getParameter(property.toString());
					if(value != null)
						values.put(property.toString(), value);
				}
			}
		}

		return new OperationInfo(getMemberCode(req),
				req.getParameter(Property.passCode.toString()), values, opr);
	}

	private String getMemberCode(HttpServletRequest req) {
		String memCode = req.getParameter(Property.memberCode.toString());
		return memCode == null ? req.getSession().getAttribute(WNUserService.CURRENT_USER)+"" : memCode;
	}

}
